<?php

class Options {

    public $siteUrl;
    public $name_start;    //название товара
    public $name_end;
    public $art_start;     //артикул
    public $art_end;
    public $price_start;   //цена
    public $price_end;
    public $desc_start;    //описание
    public $desc_end;
    public $har_start;
    public $har_end;
    public $check;
    public $img_start;     //картинки
    public $img_end;
    public $search;    //Замена в урле для большой картинки
    public $replace;
    protected static $instance;

    private function __construct($site_url) {
        $this->siteUrl = $site_url;
    }
    
    public static function getInstance($site_url) {
        if ( is_null(self::$instance) ) {
            self::$instance = new self($site_url);
        }
        return self::$instance;
    }
    
    public function init($name_start, $name_end, $art_start, $art_end, $price_start, $price_end, $desc_start, $desc_end, $har_start, $har_end, $check, $img_start, $img_end, $search, $replace) {
        $this->name_start = $name_start;
        $this->name_end = $name_end;
        $this->art_start = $art_start;
        $this->art_end = $art_end;
        $this->price_start = $price_start;
        $this->price_end = $price_end;
        $this->desc_start = $desc_start;
        $this->desc_end = $desc_end;
        $this->har_start = $har_start;
        $this->har_end = $har_end;
        $this->check = $check;
        $this->img_start = $img_start;
        $this->img_end = $img_end;
        $this->search = $search;
        $this->replace = $replace;
    }

    public function SaveSettings() {
        $query = 'REPLACE INTO `settings` 
            SET
                `domain` ="' . $this->siteUrl . '", 
                `name_start` ="' . $this->name_start . '",
                `name_end` ="' . $this->name_end . '", 
                `art_start` ="' . $this->art_start . '", 
                `art_end` ="' . $this->art_end . '", 
                `price_start` ="' . $this->price_start . '", 
                `price_end` ="' . $this->price_end . '", 
                `desc_start` ="' . $this->desc_start . '", 
                `desc_end` ="' . $this->desc_end . '",
                `har_start` ="' . $this->har_start . '", 
                `har_end` ="' . $this->har_end . '", 
                `img_start` ="' . $this->img_start . '", 
                `img_end` ="' . $this->img_end . '",
                `search` = "' . $this->search . '",
                `replace` = "' . $this->replace . '"';
        mysql_query($query);
    }

    public static function getSettings() {
        $rows = array();
        $query = 'SELECT `domain` FROM `settings`;';
        $res = mysql_query($query);
        while ($row = mysql_fetch_assoc($res)) {
            $rows[] = $row;
            $rows['i']++;
        }
        echo json_encode($rows);
    }

    #загружает настройки

    public function loadSettings() {       
        $query = 'SELECT * FROM `settings` WHERE `domain`="' . $this->siteUrl . '";';
        $res = mysql_query($query);
        if (!$res)
            die("Error: " . mysql_error());
        while ($row = mysql_fetch_assoc($res)) {            
            $this->name_start = $row['name_start'];
            $this->name_end = $row['name_end'];
            $this->art_start = $row['art_start'];
            $this->art_end = $row['art_end'];
            $this->price_start = $row['price_start'];
            $this->price_end = $row['price_end'];
            $this->desc_start = $row['desc_start'];
            $this->desc_end = $row['desc_end'];
            $this->har_start = $row['har_start'];
            $this->har_end = $row['har_end'];
            $this->img_start = $row['img_start'];
            $this->img_end = $row['img_end'];
            $this->search = $row['search'];
            $this->replace = $row['replace'];          
            $rows[] = $row;
        }
        echo json_encode($rows);
    }

}

?>
